package com.lry.basic.algorithm.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/12/26
 */
public class LetterCombation {
//    给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。
//    给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
//    输入："23"
//    输出：["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
    Map<Character, String[]> map = new HashMap();
    {
        map.put('2',new String[]{"a","b","c"});
        map.put('3',new String[]{"d","e","f"});
        map.put('4',new String[]{"g","h","i"});
        map.put('5',new String[]{"j","k","l"});
        map.put('6',new String[]{"m","n","o"});
        map.put('7',new String[]{"p","q","r","s"});
        map.put('8',new String[]{"t","u","v"});
        map.put('9',new String[]{"w","x","y","z"});
    }
    public List<String> letterCombinations(String digits) {
        if(null==digits||digits.length()==0) return null;
        char[]charArr = digits.toCharArray();
        List<String>result = new ArrayList();
        List<String>tmp = new ArrayList();
        List<String[]> res = new ArrayList();
        for(int i=0;i<charArr.length;i++){
            res.add(map.get(charArr[i]));
        }
        for(String str:res.get(0)){
            tmp.add(str);
        }
        int index = 1;
        while(index<res.size()){
            for(String tmStr:tmp){
                for(String str:res.get(index)){
                    result.add(tmStr+str);
                }
            }
            tmp.clear();
            tmp.addAll(result);
            result.clear();
            index++;
        }
        return tmp;
    }
}



